package com.example.communist_app_api.controller;



import com.example.communist_app_api.db.pojoDto.Permission;
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.sensitive.MyDdWordAllow;
import com.example.communist_app_api.sensitive.MyDdWordDeny;
import com.example.communist_app_api.sensitive.SensitiveWordService;
import com.example.communist_app_api.service.PermissionService;
import com.example.communist_app_api.service.UserRoleService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.service.WordService;
import com.example.communist_app_api.util.BASE64DecodedMultipartFile;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
<<<<<<< HEAD
<<<<<<< HEAD
import com.example.communist_app_api.websocket.WebSocketServer;
=======
>>>>>>> parent of aec1d46 (消息模块的搭建和配合其他模块消息的发送和收取)
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
=======
>>>>>>> 党建uniapp/dev
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;
<<<<<<< HEAD
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
<<<<<<< HEAD
import java.util.*;
import java.util.stream.Collectors;
=======
import java.util.ArrayList;
>>>>>>> 党建uniapp/dev
=======
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
>>>>>>> parent of aec1d46 (消息模块的搭建和配合其他模块消息的发送和收取)

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/9/27 17:16
 */
@RestController
@RequestMapping("/test")
@Api("测试Web接口")
public class TestController {

    @Autowired
    private SensitiveWordService sensitiveWordService;
    @PostMapping("/sayHello")
    @ApiOperation("最简单的测试方法")
    public R sayHello(@Valid @RequestBody String name){
        return R.ok().put("message","Hello,"+name);
    }

    @PostMapping("/testIOImageFile")
    @ApiOperation("测试图像文件流")
    public void putImageFile(){
        //直接上base64地址
        long startTime=System.currentTimeMillis();
        MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart("");
        String s = CosUtils.uploadFile(multipartFile, "");
        long endTime=System.currentTimeMillis();
        Long time=endTime-startTime;
        System.out.println("s="+s);
        System.out.println("耗时："+time);
    }

    @Autowired
    private UserService userService;
    @GetMapping("/get-user")
    public R getUser(@RequestParam String username){
        User user = userService.getUserInfo(username);
        return R.ok().put("User",user);
    }

    @Autowired
    private UserRoleService userRoleService;
    @GetMapping("/getRoleId")
    @PreAuthorize("hasAuthority('communist_manage_select')")
    public R getUserRole(@RequestParam Integer userId){
        ArrayList<Integer> userIdList = userRoleService.getRoleIdByUserId(userId);
        return R.ok().put("roleId",userIdList);
    }
    //PreAuthorize前置判断是否有权限   hasAuthority判断是否有权限
    //@PreAuthorize("hasAuthority('communist_manage_update')")
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/getPermission")
    @PreAuthorize("hasAuthority('communist_manage_update')")
    public R getPermission(@RequestParam Integer permissionId){
        Permission permission = permissionService.getPermissionByPermissionId(permissionId);
        return R.ok().put("permission",permission);
    }

    @Autowired
    private MyDdWordAllow myDdWordAllow;

    @Autowired
    private MyDdWordDeny myDdWordDeny;

    @Autowired
    private WordService wordService;

    @PostMapping("/testSensitiveWord")
    @ApiOperation("测试敏感词")
    public R testSensitiveWord(@RequestParam String text, @RequestHeader("token") String token){
//        //每次程序运行都会初始化init
//        Long startTime = System.currentTimeMillis();
//        SensitiveWordBs wordBs = SensitiveWordBs.newInstance()
//                .ignoreCase(true)
//                .ignoreWidth(true)
//                .ignoreNumStyle(true)
//                .ignoreChineseStyle(true)
//                .ignoreEnglishStyle(true)
//                .ignoreRepeat(false)
//                .enableNumCheck(true)
//                .enableEmailCheck(true)
//                .enableUrlCheck(true)
//                .numCheckLen(8)
//                //使用自定义词库
//                .wordDeny(myDdWordDeny)
//                .wordAllow(myDdWordAllow);
//        boolean contains = wordBs.contains(text);
//        //boolean contains = SensitiveWordHelper.contains(text);
//        //true为有敏感词 false为不符合
//        //final String text = "五星红旗迎风飘扬，毛主席的画像屹立在天安门前。";
//        //Assert.assertTrue(wordBs.contains(text));
//        List<String> allSensitive = wordBs.findAll(text);
//        //List<String> allSensitive = SensitiveWordHelper.findAll(text);
//        String replace = wordBs.replace(text);
//        //String replace = SensitiveWordHelper.replace(text);
//        Long endTime = System.currentTimeMillis();
//        if (!contains ) {
//            return R.ok().put("text",text).put("time",endTime-startTime);
//        }else {
//            return R.error("不符合").put("allSensitive",allSensitive).put("replace",replace).put("time",endTime-startTime);
//        }
        Integer receiverId = getUserIdByTokenAndUniApp(token);
        SensitiveWordApiResp resp = wordService.detectSensitiveWord(text, String.valueOf(receiverId));
        Boolean isSensitiveWord = resp.getIsSensitiveWord();
        if (isSensitiveWord) {
            return R.error("含有敏感词");
        }else {
            return R.ok("正常").put("text",text);
        }
    }

    private  Integer getUserIdByTokenAndWeb(@RequestHeader("token") String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String username = claims.getSubject();
        User userInfo = userService.getUserInfo(username);
        return userInfo.getId();
    }

    private Integer getUserIdByTokenAndUniApp(@RequestHeader("token") String token){
        Claims claims = JwtUtils.parseJWT(token);
        String id_String = claims.getSubject();
        int userId = Integer.parseInt(id_String);
        return userId;
    }

}

